package news.yp.newsproject.presenter.presenter;

import com.news.yp.baselibrary.base.BasePresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import news.yp.newsproject.bean.RequestDetailData;
import news.yp.newsproject.bean.ResponseDetailData;
import news.yp.newsproject.presenter.model.ModelPresenter;

public class DetailPresenter extends BasePresenter<CenterPresenter.DetailViewPresenter> {

    private final ModelPresenter mModel;

    public DetailPresenter() {
        mModel = new ModelPresenter();
    }

    public void getDetailData(final RequestDetailData requestDetailData) {
        getView().onLoading();
        mModel.getDetailData(requestDetailData)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseDetailData>() {
                    @Override
                    public void accept(ResponseDetailData responseDetailData) throws Exception {
                        if (responseDetailData == null) {
                            throw new Exception("返回数据为空");
                        }
                        if (responseDetailData.getCode() == 1) {
                            throw new Exception("没有数据");
                        }
                        getView().onDetailSuccess(responseDetailData);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().onError();
                    }
                });
    }

}
